package com.antaohua.platfrom.framework.web.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.antaohua.platfrom.framework.base.exception.DeleteEntityException;
import com.antaohua.platfrom.framework.base.exception.InsertEntityException;
import com.antaohua.platfrom.framework.base.exception.QureyEntityException;
import com.antaohua.platfrom.framework.base.exception.UpdateEntityException;
import com.antaohua.platfrom.framework.base.interfase.BaseService;
import com.antaohua.platfrom.framework.base.model.UUIDIdentityModel;
import com.antaohua.platfrom.framework.base.utils.PageUtil;
import com.google.common.collect.Lists;


public abstract class BaseController<E extends UUIDIdentityModel>{

	protected Log log = LogFactory.getLog(getClass());

	
	protected abstract BaseService<E> getService();
	
	
	@RequestMapping(path="save")
	public @ResponseBody E save(E entity)throws  InsertEntityException{
		
		E e = getService().save(entity);
		return e;
		
	}

	
	@RequestMapping(path="update")
	public @ResponseBody String update(E entity)throws UpdateEntityException {
		getService().update(entity);
		return String.valueOf(true);
	}

	
	@RequestMapping(path="get")
	public @ResponseBody E getById(String id) throws QureyEntityException{
		if(id!=null&&id.length()>0){
			return getService().findById(id);
		}else{
			throw new QureyEntityException("ID IS NULL");
		}
		
	}

	
	@RequestMapping(path="find_all_page")
	public @ResponseBody PageUtil<E> findAll(PageUtil<E> page) throws QureyEntityException {
		return getService().findAllFotPage(page);
	}

	
	@RequestMapping(path="find_all")
	public @ResponseBody List<E> findAll() throws QureyEntityException {
		
		return getService().findAll();
	}

	
	@RequestMapping(path="find_propertys")
	public @ResponseBody List<E> findByProperty(ModelMap propertys) throws QureyEntityException {
		return getService().findByPropertys(propertys);
	}

	
	@RequestMapping(path="find_property")
	public @ResponseBody List<E> findByProperty(String propertyName, String value) throws QureyEntityException {
		return getService().findByProperty(propertyName,value);
	}

	
	@RequestMapping(path="count_property")
	public @ResponseBody Long findCountByProperty(String propertyName, String value) throws QureyEntityException {
		
		return getService().countByProperty(propertyName,value);
	}

	
	@RequestMapping(path="count")
	public @ResponseBody Long rowCount() throws QureyEntityException {
		
		return getService().count();
	}

	
	@RequestMapping(path="delete")
	public @ResponseBody String delete(E entity) throws DeleteEntityException {
		getService().delete(entity);
		return String.valueOf(true);
	}

	
	@RequestMapping(path="delete_id")
	public @ResponseBody Integer deleteById(String id) throws DeleteEntityException {
		return getService().deleteById(id);
	}


	
	@RequestMapping(path="delete_ids")
	public @ResponseBody Integer deleteById(String[] ids) throws DeleteEntityException {
		
		return getService().deleteByIds(ids);
	}
	
	
	
	
}
